﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.DAL.DTOs;
using MS.InformationManagement.Core.TransactionsResponse;

namespace MS.InformationManagement.BLL.DeliveryForms
{
    public class DeliveryFormsCommand : ICommandWithResponse
    {

        #region Fields

        Stack<ICommand> _commands;
        Planilla _deliveryForm;
        List<DeliveryFormDTO> _deliveryFormsRow;
        string _entityName;

        #endregion

        #region Constructors

        public DeliveryFormsCommand(Planilla deliveryForm, List<DeliveryFormDTO> deliveryFormRows, string entityName)
        {
            _deliveryForm = deliveryForm;
            _deliveryFormsRow = deliveryFormRows;
            _entityName = entityName;
        }

        #endregion

        #region Command Methods

        public ITransactionResponse Execute()
        {
            _commands = new Stack<ICommand>();

            try
            {

                //Delivery Form Creation
                CreateDeliveryFormCommand deliveryCreation = new CreateDeliveryFormCommand(_deliveryForm);
                _commands.Push(deliveryCreation);
                deliveryCreation.Execute();

                //Delivery form html creation
                DeliveryHtmlFormCreatorCommand deliveryHtmlCreation = new DeliveryHtmlFormCreatorCommand(_deliveryFormsRow, _entityName, _deliveryForm.Id.ToString());
                _commands.Push(deliveryHtmlCreation);
                DeliveryFormHtmlCreationResponse htmlCreationResponse = deliveryHtmlCreation.Execute() as DeliveryFormHtmlCreationResponse;
                
                //Delivery form document creation
                DeliveryFormDocumentCreatorCommand deliveryDocumentCreation = new DeliveryFormDocumentCreatorCommand(htmlCreationResponse.HtmlResponse);
                _commands.Push(deliveryDocumentCreation);
                DeliveryFormCreationResponse documentResponse = deliveryDocumentCreation.Execute() as DeliveryFormCreationResponse;
                documentResponse.DocumentName = _deliveryForm.Id.ToString() + ".pdf";

                //Attach delivery form document
                AttachDeliveryDocumentCommand attachDocumentCommand = new AttachDeliveryDocumentCommand(documentResponse.DeliveryContent, _deliveryForm.Id.Value, documentResponse.DocumentName);
                _commands.Push(attachDocumentCommand);
                attachDocumentCommand.Execute();
                
                //Change radicado status
                UpdateDeliveryRadicadoCommand updateRadicado = new UpdateDeliveryRadicadoCommand(_deliveryForm);
                _commands.Push(updateRadicado);
                updateRadicado.Execute();

                //Change deliveries status
                MarkDeliveriesAsSentCommand markAsSentCommand = new MarkDeliveriesAsSentCommand(_deliveryForm.Envios);
                _commands.Push(markAsSentCommand);
                markAsSentCommand.Execute();

                //Delete WindowReception
                DeleteWindowReceptionCommand deleteReceptionCommand = new DeleteWindowReceptionCommand(_deliveryForm.Envios);
                deleteReceptionCommand.Execute();

                return documentResponse;

            }
            catch (Exception ex)
            {
                this.Undo();
                throw ex;
            }
        }

        public void Undo()
        {
            foreach (var item in _commands)
                item.Undo();
        }

        #endregion

    }
}
